package cn.ztuo.bitrade.controller;


import cn.ztuo.bitrade.entity.ContractBond;
import cn.ztuo.bitrade.entity.ContractCoin;
import cn.ztuo.bitrade.entity.ContractPosition;
import cn.ztuo.bitrade.entity.Member;
import cn.ztuo.bitrade.entity.transform.AuthMember;
import cn.ztuo.bitrade.service.ContractPositionService;
import cn.ztuo.bitrade.service.MemberService;
import cn.ztuo.bitrade.util.ContractUtils;
import cn.ztuo.bitrade.util.MessageResult;
import cn.ztuo.bitrade.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.math.BigDecimal;
import java.util.List;

import static cn.ztuo.bitrade.constant.SysConstant.SESSION_MEMBER;

@Slf4j
@RestController
@RequestMapping(value="contract-position")
public class ContractPositonController extends BaseController{
    @Autowired
    private ContractPositionService positionService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private MemberService memberService;


    /**
     * 查询默认交易对儿
     * @return
     */
    @RequestMapping(value = "/findPositions",method = RequestMethod.POST)
    public MessageResult findPositions(@SessionAttribute(SESSION_MEMBER) AuthMember user, ContractPosition position){
            List<ContractPosition> list = positionService.findPositions(position);
            return success(list);
    }


    /**
     * 修改仓位杠杆
     *
     */
    @RequestMapping("/updateMultiple")
    public MessageResult updateMultiple(@SessionAttribute(SESSION_MEMBER) AuthMember member,String symbol,String multiple) {
        if(StringUtils.isBlank(multiple)||StringUtils.isBlank(symbol)){
            return MessageResult.error("请选择币种和杠杆倍数");
        }
        List<ContractPosition> positions = positionService.findByMemberIdAndSymbol(member.getId(),symbol);
        ContractCoin coin = ContractUtils.getBaseSymbol(symbol);

        for (ContractPosition position :positions) {
            position.setCurrentMultiple(multiple);
            position = positionService.saveAndFlush(position);
//            if(position!=null){
//                JSONObject json = ContractUtils.getStart(position.getPositionTotalValue(),coin,null,member.getId(),new BigDecimal(multiple));
//                position.getHoldingBond().subtract(json.getBigDecimal("start"));
//                position.setHoldingBond(json.getBigDecimal("start"));
//
//            }
        }
        return MessageResult.success() ;
    }

    /**
     * 查询用户当前持仓 杠杆;
     * @return
     */
    @RequestMapping(value = "/getPositionCurrentMultiple",method = RequestMethod.POST)
    public MessageResult getPositionCurrentMultiple(@SessionAttribute(SESSION_MEMBER) AuthMember user, String symbol){
            List<ContractPosition> list = positionService.findByMemberIdAndSymbol(user.getId(),symbol);

            String currentMultiple = null ;
            if(list!=null&&list.size()>0){
                currentMultiple = list.get(0).getCurrentMultiple();
            }
            if(StringUtils.isBlank(currentMultiple)){
                ContractCoin coin = ContractUtils.getBaseSymbol(symbol);
                BigDecimal totalBond = ContractUtils.getTotalBond(user.getId(),coin);
                ContractBond bond = ContractUtils.getBond(null,coin,user.getId(),null);
                currentMultiple = bond.getMultiple();
            }
            return success("",currentMultiple);
    }
    /**
     * 同意风险
     * @return
     */
    @RequestMapping(value = "/confirmUserRiskTipsStatus")
    public MessageResult confirmUserRiskTipsStatus(@SessionAttribute(SESSION_MEMBER) AuthMember user, Boolean isAgree){
        Member member = memberService.findOne(user.getId());
        member.setIsAgreeContract(isAgree);
        member = memberService.saveAndFlush(member);
        return success("",member);
    }

    /**
     * 是否同意风险
     * @return
     */
    @RequestMapping(value = "/isAgree")
    public MessageResult isAgree(@SessionAttribute(SESSION_MEMBER) AuthMember user){
        Member member = memberService.findOne(user.getId());
        List<ContractPosition> list = positionService.findByMemberId(user.getId());
        if(list!=null&&list.size()>0){
            return success("",true);
        }
        return success("",member.getIsAgreeContract());
    }
}
